package cn.iocoder.yudao.module.member.vip.service;

import cn.iocoder.yudao.framework.business.basic.dao.query.CoreQueryWrapper;
import cn.iocoder.yudao.framework.business.basic.service.CrudService;
import cn.iocoder.yudao.framework.business.basic.service.impl.BaseService;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.api.member.vip.enums.VipTypeEnum;
import cn.iocoder.yudao.module.member.vip.dal.dataobject.VipCardDO;
import cn.iocoder.yudao.module.member.vip.dal.mysql.VipCardMapper;
import cn.iocoder.yudao.module.member.vip.dto.VipCardQueryDTO;
import cn.iocoder.yudao.module.member.vip.dto.VipCardSaveDTO;
import cn.iocoder.yudao.module.member.vip.vo.VipCardVO;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

/**
 * 会员卡 Service 实现类
 *
 * @author 山野羡民
 */
@Service
@Validated
public class VipCardServiceImpl extends BaseService<VipCardDO, VipCardVO, VipCardQueryDTO> implements VipCardService, CrudService<VipCardDO, VipCardVO, VipCardSaveDTO, VipCardQueryDTO> {
    @Resource
    private VipCardMapper vipCardMapper;

    @Override
    public List<VipCardVO> getList(boolean isMerchant, @Nullable Integer limit) {
        Integer type = (isMerchant ? VipTypeEnum.MERCHANT : VipTypeEnum.MEMBER).getValue();
        List<VipCardDO> list = getInjectedMapper().selectList(type, limit);
        return BeanUtils.toBean(list, VipCardVO.class);
    }

    @Nonnull
    @Override
    protected CoreQueryWrapper<VipCardDO> buildQueryWrapper(VipCardQueryDTO param) {
        CoreQueryWrapper<VipCardDO> wrapper = super.buildQueryWrapper(param);
        wrapper.likeIfPresent("name", param.getName());
        wrapper.eqIfPresent("type", param.getType());
        return wrapper;
    }

    @Override
    public VipCardMapper getInjectedMapper() {
        return vipCardMapper;
    }

    @Override
    public VipCardDO convertToEntity(VipCardSaveDTO param) {
        return BeanUtils.toBean(param, VipCardDO.class);
    }

}